#!/usr/bin/perl
#
$K = $ARGV[0];
while($line=<STDIN>) {
	chomp($line);
	($a,$b,$left,$right) = split(/\s+/,$line);
	if($bestOrder{$left}<$K) {
		$bestOrder{$left}++;
		$present{"$left\t$right"} = $b;
	}
}

foreach $key (sort {$present{$b}<=>$present{$a}} keys %present) {
	print "\t".$present{$key}."\t".$key."\n";
}

